package com.clinic.web.util;

import java.io.StringWriter;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Random;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.Version;

public class Util {

	public static String GET_Method(String URL) {
		String str_out = "";
		try {
			Client client = Client.create();
			WebResource webResource = client.resource(URL);
			ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);

			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
			}

			str_out = response.getEntity(String.class);
			//System.out.println("Output from Server ....");
			System.out.println(str_out);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return str_out;
	}

	public static String POST_Method(String URL, String json_input) {
		System.out.println("Enter - POST_Method");
		String str_out = "";
		try {
			Client client = Client.create();
			WebResource webResource = client.resource(URL);
			System.out.println("JSON : " + json_input);
			ClientResponse response = webResource.type("application/json").post(ClientResponse.class, json_input);

			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
			}
			//System.out.println("Output from Server ....");
			str_out = response.getEntity(String.class);
			System.out.println(str_out);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		System.out.println("Exit - POST_Method");
		return str_out;
	}

	public static void sendmail(String To, String Subject, String Content) {
		System.out.println("Enter - sendmail");
		//To = "clinic.project2014@gmail.com";
		String From = "clinic.project2014@gmail.com";

		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", "smtp.gmail.com");
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.smtp.port", "" + 587);
		props.setProperty("mail.smtp.starttls.enable", "true");
		Session session = Session.getDefaultInstance(props,	new Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication("clinic.project2014@gmail.com", "MohamedLabib");
					}
			});

		try {
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(From));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(To));
			message.setSubject(Subject);
			message.setContent(Content, "text/html");
			// Send message
			Transport.send(message);
		} catch (MessagingException mex) {
			mex.printStackTrace();
		}
		System.out.println("Exit - sendmail");
	}
	
	public static Long generate_token(){
		System.out.println("Enter - generate_token");
		Random r = new Random();
		System.out.println("Exit - generate_token");
		return r.nextLong();
	}
	
	public static void sendmailbytemplate(String To, String Subject, String templateName, Map<String, Object> inputmap) throws Exception {
		System.out.println("Enter - sendmailbytemplate");
		Configuration cfg = new Configuration();

		cfg.setClassForTemplateLoading(Util.class, "templates");
		cfg.setIncompatibleImprovements(new Version(2, 3, 20));
		cfg.setDefaultEncoding("UTF-8");
		cfg.setLocale(Locale.US);
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
		
		Template template = cfg.getTemplate(templateName);
		StringWriter writer = new StringWriter();
		template.process(inputmap, writer);

		System.out.println(writer.toString());
		sendmail(To, Subject, writer.toString());
		System.out.println("Exit - sendmailbytemplate");
	}
}